# author：马楚云
# 进行比较处理的类
class Comparator(object):
    def __init__(self):
        self.__operator_map = {
            'lt': self.__comparator_lt,
            'bt': self.__comparator_bt,
            'sc': self.__comparator_sc,
            'se': self.__comparator_se,
            'ne': self.__comparator_ne,
            'gt': self.__comparator_gt,
            'eq': self.__comparator_eq,
            'between': self.__comparator_between
        }
        self.__sc_flag = False

    def __set_role_sid_map(self):
        # 服务对应的sid
        sid_role_map = {
            'S-1-5-6': 'SERVICE', 'S-1-5-21domain-501': 'Guests', 'S-1-5-32-544': 'Administrators',
            'S-1-5-20': 'NETWORK SERVICE',
            'S-1-5-32-555': 'Remote Desktop Users',
            'S-1-1-0': 'Everyone', 'S-1-5-32-545': 'Users', 'S-1-5-32-551': 'Backup Operators',
            'S-1-5-19': 'LOCAL SERVICE', '': 'Local account', 'S-1-5-80': 'NT SERVICE\\WdiServiceHost',
            'S-1-5-32-568': 'IIS_IUSRS', 'S-1-5-80-0':'Administrators',
            'S-1-5-32-559': 'Performance Log Users',
            'S-1-5-80-3139157870-2983391045-3678747466-658725712-1809340420': 'NT SERVICE\WdiServiceHost'
        }
        return sid_role_map

    # 小于比较符lt
    def __comparator_lt(self, config_val, proposed_val):
        return int(config_val) < int(proposed_val)

    # 大于比较符bt
    def __comparator_bt(self, config_val, proposed_val):
        return int(config_val) > int(proposed_val)

    # 处于区间之中比较符between
    def __comparator_between(self, config_val, proposed_val):
        border = proposed_val.split(",")
        return config_val in range(int(border[0]), int(border[1]) + 1)

    # 特殊规则的比较符sc Service Compare
    def __comparator_sc(self, config_val, proposed_val):
        # Flag 用于给compare函数返回需要的config_val
        if proposed_val == 'disabled':
            return '4' == config_val    # 禁止的状态码是‘4’
        elif proposed_val == 'disabled,notInstalled':
            comp_res = True if config_val == '4' or config_val is None else False
            return comp_res

    # 等于比较符eq
    def __comparator_eq(self, config_val, proposed_val):
        return config_val == proposed_val

    # 用户权限序列比较符se Sequence Compare
    def __comparator_se(self, config_val, proposed_val):
        config_roles = self.get_config_roles(config_val)
        config_roles = set(config_roles)
        proposed_val = [val.lower() for val in proposed_val.split(',')]
        proposed_roles = set(proposed_val)
        comp_res = '符合' if config_roles == proposed_roles else '不符合'
        return comp_res

    # 获取配置项
    def get_config_roles(self, config_val):
        sid_role_map = self.__set_role_sid_map()
        config_roles = []
        if config_val == 'Key not exist':
            return "未检测到配置信息"
        elif type(config_val) == list:
            try:
                for role in config_val:
                    config_roles.append(sid_role_map[role[1:]].lower())
            except KeyError:
                pass
        elif type(config_val) == str:
            try:
                config_roles = [sid_role_map[config_val[1:]].lower()]
            except KeyError:
                config_roles = [config_val.lower()]
        return config_roles

    # 不等于比较符ne
    def __comparator_ne(self, config_val, proposed_val):
        if proposed_val is None:
            return config_val != ""
        return config_val != proposed_val

    # 多项比较符gt
    def __comparator_gt(self, config_val, proposed_val):
        proposed_vals = proposed_val.split(",")
        return config_val[0] >= proposed_vals[0] or config_val[1] > proposed_vals[1]

    # 返回比较结果
    def compare(self, config_val, proposed_val, operator):
        comparator_fun = self.__operator_map[operator]
        comp_res = '符合' if comparator_fun(config_val, proposed_val) else '不符合'
        return comp_res

